//*********************************************************
//
//    Copyright (c) Microsoft. All rights reserved.
//    This code is licensed under the Apache License, Version 2.0.
//    THIS CODE IS PROVIDED *AS IS* WITHOUT WARRANTY OF
//    ANY KIND, EITHER EXPRESS OR IMPLIED, INCLUDING ANY
//    IMPLIED WARRANTIES OF FITNESS FOR A PARTICULAR
//    PURPOSE, MERCHANTABILITY, OR NON-INFRINGEMENT.
//
//*********************************************************

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Drawing.Imaging;
using System.Data;
using System.Text;
using System.Windows.Forms;

namespace Microsoft.Research.DataLayer
{
    public partial class ConnMgrDlg_Button : UserControl
    {
        public ConnMgrDlg_Button()
        {
            InitializeComponent();
        }

        public ConnMgrDlg_Button(string txt, Image img)
        {
            InitializeComponent();

            if (txt != null && img != null)
            {
                this.product.Text = txt;
                this.Name = System.Text.RegularExpressions.Regex.Replace(txt, @"[\@\s""'()*&%$#!]", "_");
                this.logo.Image = img;
            }
            else if (txt == null)
            {
                this.product.Text = "";
                this.logo.Image = img;
                this.logo.Width = this.Width - this.logo.Left;
            }
        }

        private bool selected = false;
        public bool Selected
        {
            get { return selected; }
            set
            {
                selected = value;
                this.Invalidate();
            }
        }

        public event EventHandler OnSelected;

        public Image Logo
        {
            get { return logo.Image; }
        }


        private void ConnMgrDlg_Button_Load(object sender, EventArgs e)
        {
            this.MinimumSize = this.Size;
            this.MaximumSize = this.Size;
        }

        private void ConnMgrDlg_Button_Paint(object sender, PaintEventArgs e)
        {
            if (selected)
            {
                e.Graphics.Clear(Color.LightBlue);
            }

            Rectangle drawRect = new Rectangle(this.logo.Left, this.logo.Top,
                this.logo.Width - 2 * this.logo.Left, this.logo.Height - this.logo.Top);

            if (drawRect.Size.Width > this.logo.Image.Size.Width)
            {
                int delta = (this.logo.Image.Size.Width - drawRect.Size.Width) / 2;
                drawRect.Inflate(delta, 0);
            }

            if (drawRect.Size.Height > this.logo.Image.Size.Height)
            {
                int delta = (this.logo.Image.Size.Height - drawRect.Size.Height) / 2;
                drawRect.Inflate(0, delta);
            }

            SizeF textArea = new SizeF(this.Width - this.logo.Right - 4, this.logo.Height);
            SizeF textSize = e.Graphics.MeasureString(this.product.Text, this.Font, textArea);
            Rectangle textRect = new Rectangle(
                this.logo.Right,
                (int)(this.product.Top + (this.product.Height - textSize.Height) / 2),
                (int)textSize.Width + 1, (int)textSize.Height + 1);

            if (!Enabled)
            {
                //
                // Grayscale the image
                //
                ColorMatrix colorMatrix = new ColorMatrix(new float[][] 
                {
                    new float[] {.3f, .3f, .3f, 0, 0},
                    new float[] {.59f, .59f, .59f, 0, 0},
                    new float[] {.11f, .11f, .11f, 0, 0},
                    new float[] {0, 0, 0, 1, 0},
                    new float[] {0, 0, 0, 0, 1}
                });
                ImageAttributes attributes = new ImageAttributes();
                attributes.SetColorMatrix(colorMatrix);

                Image src = this.logo.Image;

                e.Graphics.DrawImage(src,
                    new Rectangle(drawRect.X, drawRect.Y, drawRect.Width, drawRect.Height),
                    0, 0, src.Width, src.Height,
                    GraphicsUnit.Pixel, attributes);

                e.Graphics.DrawString(this.product.Text,
                    this.Font, new SolidBrush(this.ForeColor),
                    textRect);
            }
            else
            {
                e.Graphics.DrawImage(this.logo.Image, drawRect);

                e.Graphics.DrawString(this.product.Text,
                    this.Font, new SolidBrush(this.ForeColor),
                    textRect);
            }
        }

        private void ConnMgrDlg_Button_Click(object sender, EventArgs e)
        {
            if (OnSelected != null)
                OnSelected(this, new EventArgs());
        }

        private void ConnMgrDlg_Button_EnabledChanged(object sender, EventArgs e)
        {
            this.logo.Enabled = this.Enabled;
        }
    }
}